﻿using CoreShop.App.Units;
using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Services;
using CoreShop.Services.Services.Report;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using static CoreShop.Library.GlobalEnumVars;

namespace CoreShop.App
{
    public partial class frmMarket : UIForm
    {
        public bool returnValue;

        List<Reportscorecmsfoodordermaster> lstSource = new List<Reportscorecmsfoodordermaster>();
        public delegate void CloseFormPage(int pageIndex);
        public event CloseFormPage SetCloseFormPage;//新订单统计，frmMainWin.btn_NewBill
        public int mPageIndex = 0;
        public string number = "";

        long foodTypeId = 0;
        string foodTypeName = "";
        public frmMarket(int _mPageIndex)
        {
            mPageIndex = _mPageIndex;
            InitializeComponent();
        }
        public frmMarket(string _number)
        {
            number = _number;
            InitializeComponent();
        }
        public frmMarket()
        {
            InitializeComponent();
        }

        #region 加载信息列表
        private void LoadDataList()
        {
            DateTime dtStartDate = CommFunc.ConvertToDateTime(dateTimeRange.DateTimeStar);
            DateTime dtEndDate = CommFunc.ConvertToDateTime(dateTimeRange.DateTimeEnd);
            int count = 0;
            Reportscorecmsfoodordermaster sumRow = new Reportscorecmsfoodordermaster();

            var marketId = CommFunc.ConvertToInt32(orderClass.SelectedValue);

            lstSource = new corereportServices().Getcorecmsfoodordermaster(ref count, mPageIndex, btnPg.PageSize, dtStartDate, dtEndDate, marketId, ref sumRow);
            btnPg.TotalCount = count;
            this.dgvPayList.AutoGenerateColumns = false;
            this.dgvPayList.DataSource = lstSource;
            //uiDataGridViewFooter1.DataGridView = sumRow;
            //设置统计绑定的表格
            uiDataGridViewFooter1.DataGridView = dgvPayList;
            uiDataGridViewFooter1["orderDate"] = "合计：";
            uiDataGridViewFooter1["sumRealAmount"] = CommFunc.ConvertToString(sumRow.sumRealAmount);
            uiDataGridViewFooter1["sumReduceAmount"] = CommFunc.ConvertToString(sumRow.sumReduceAmount);
            uiDataGridViewFooter1["orderAmount"] = CommFunc.ConvertToString(sumRow.orderAmount);
            uiDataGridViewFooter1["orderCount"] = CommFunc.ConvertToString(sumRow.orderCount);
            uiDataGridViewFooter1["strDiscountOrder"] = CommFunc.ConvertToDecimal(sumRow.strDiscountOrder).ToString("0.00");
            uiDataGridViewFooter1["endDiscountOrder"] = CommFunc.ConvertToDecimal(sumRow.endDiscountOrder).ToString("0.00");
            uiDataGridViewFooter1["sumQuantity"] = CommFunc.ConvertToString(sumRow.sumQuantity);
            uiDataGridViewFooter1["strDiscountPeople"] = CommFunc.ConvertToDecimal(sumRow.strDiscountPeople).ToString("0.00");
            uiDataGridViewFooter1["endDiscountPeople"] = CommFunc.ConvertToDecimal(sumRow.endDiscountPeople).ToString("0.00");
        }
        #endregion
        private void Food_Load(object sender, System.EventArgs e)
        {
            #region 加载餐段
            this.orderClass.Items.Clear();
            var memberSex = EnumHelper.EnumToList<GlobalEnumVars.Payment>();
            List<corecmsstoremarket> markGrid = new List<corecmsstoremarket>();
            var market0 = new corecmsstoremarket();
            market0.id = 0;
            market0.name = "全部";
            List<corecmsstoremarket> markGrid1 = new corecmsstoremarketServices().QueryAsync();
            markGrid.Add(market0);
            markGrid.AddRange(markGrid1);
            orderClass.DataSource = markGrid;
            orderClass.DisplayMember = "name";
            orderClass.ValueMember = "id";
            if (markGrid.Count > 0) this.orderClass.SelectedIndex = 0;
            #endregion
            mPageIndex = 1;
            LoadDataList();
        }
        private void btnSerach_Click(object sender, EventArgs e)
        {
            mPageIndex = 1;
            LoadDataList();
        }

        private void dgvFoodList_CellDoubleClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
        {
            //btnUpdate_Click(null, null);
        }

        private void btnPg_PageChanged(object sender, object pagingSource, int pageIndex, int count)
        {
            mPageIndex = pageIndex;
            LoadDataList();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            //btnExport_Click
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.Title = "Export Excel File To";
            saveFileDialog.ShowDialog();
            string fileName = saveFileDialog.FileName;
            if (fileName == "")
            {
                return;
            }
            Dictionary<string, DataGridView> gridViews = new Dictionary<string, DataGridView>();
            gridViews.Add("班次营业统计", dgvPayList);
            if (ExportUtil.NPOIExportForDataGridview(gridViews, fileName))
            {
                MessageBox.Show("导出Excel成功！");
            }
        }
    }
}